﻿using System;
using System.Text.RegularExpressions;
using Microsoft.Security.Application;
using System.Web;

namespace MVCLD.Helpers
{
    public static class MyExtensionsClass
    {
        public static string RemoveNonNumeric(this string s)
        {
            if (!string.IsNullOrEmpty(s))
            {
                char[] result = new char[s.Length];
                int resultIndex = 0;
                foreach (char c in s)
                {
                    if (char.IsNumber(c))
                        result[resultIndex++] = c;
                }
                if (0 == resultIndex)
                    s = string.Empty;
                else if (result.Length != resultIndex)
                    s = new string(result, 0, resultIndex);
            }
            return s;
        }

        public static string SQLInject(this string TheInput)
        {
            // See if we have a value
            if (!string.IsNullOrEmpty(TheInput))
            {
                TheInput = HttpUtility.HtmlDecode(TheInput);
                TheInput = Helpers.StripHTMLFromString(TheInput);
            }
            return TheInput;
        }

        public static int SQLInject(this int TheInput)
        {
            try
            {
                if (Helpers.IsNumeric(TheInput))
                { return TheInput; }
                else
                { return 0; }
            }
            catch
            {
                return 0;
            }
        }

        public static decimal SQLInject(this decimal TheInput)
        {
            try
            {
                if (Helpers.IsNumeric(TheInput))
                { return TheInput; }
                else
                { return 0; }
            }
            catch
            {
                return 0;
            }
        }


        public static string niceurl(this string strInput)
        {
            strInput = strInput.Replace("&amp;", "and");
            strInput = strInput.Replace("&", "and");
            strInput = Regex.Replace(strInput, "[^\\w]", "-");           
            strInput = strInput.Replace("---", "-");
            strInput = strInput.Replace("--", "-");
            if (strInput.EndsWith("-"))
            {
                strInput = strInput.Substring(0, strInput.Length - 1);
            }
            strInput = strInput.ToLower();
            return strInput;
        }


        public static int ToInt32(this string TheInput)
        {
            if (!string.IsNullOrEmpty(TheInput))
            {
                return Convert.ToInt32(TheInput);
            }
            return 0;
        }

    }
}
